// 登录框控制器
define('loginBoxController', ['angular'], function (angular) {
    var module = angular.module('controllers');
    module.controller('loginBoxController', ['$scope','$rootScope','$location','$window','loginService','AuthenticationService', function ($scope,$rootScope,$location, $window,loginService,AuthenticationService) {
        $scope.formData={};
        $scope.processForm=function(){
            var username=$scope.formData.account;
            var password=$scope.formData.password;
            $scope.logIn(username,password);
        };
        $scope.logIn = function logIn(username, password) {
            if (username !== undefined && password !== undefined) {
                loginService.logIn(username, password).then(function(data) {
                    if(data.data!="false"){
                        AuthenticationService.isLogged = true;
                        $window.sessionStorage.token = data.data.token;
                        $rootScope.user=data.data.user;
                        $window.localStorage.crm_user=JSON.stringify(data.data.user);
                        $location.path("/Notice/index");
                    }else{
                        $scope.formData.error="用户不存在或密码错误";
                    }
                },function(response){
                    console.log(response);
                });
            }
        };
        $scope.logout = function logout() {
            if (AuthenticationService.isLogged) {
                AuthenticationService.isLogged = false;
                delete $window.sessionStorage.token;
                $window.localStorage.removeItem("crm_user");
                $location.path("/login");
            }
        };
    }]);
});